package com.linkage.jz.bankruptcy.utils;

import android.app.DownloadManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Base64;

import java.io.ByteArrayOutputStream;

import android.os.Environment;

import com.linkage.jz.bankruptcy.BankruptcyApp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;

public class FileUtil {
    private static final Map<String, String> mimeTypeToExtensionMap = new HashMap<String, String>();
    private static final Map<String, String> extensionToMimeTypeMap = new HashMap<String, String>();

    static {
        add("video/*", "avi");
        add("video/*", "flv");
        add("video/*", "m4v");
        add("video/*", "mkv");
        add("video/*", "mov");
        add("video/*", "mp4");
        add("video/*", "mpg");
        add("video/*", "rm");
        add("video/*", "rmvb");
        add("video/*", "3gp");
        add("video/*", "vob");
        add("video/*", "wmv");

        add("audio/*", "aac");
        add("audio/*", "m4a");
        add("audio/*", "mp3");
        add("audio/*", "mpa");
        add("audio/*", "ogg");
        add("audio/*", "wav");
        add("audio/*", "wma");

        add("image/*", "bmp");
        add("image/*", "gif");
        add("image/*", "jpg");
        add("image/*", "jpeg");
        add("image/*", "png");
        add("image/*", "svg");
        add("image/*", "tiff");

        add("application/msword", "doc");
        add("application/msword", "docx");
        add("application/pdf", "pdf");
        add("application/vnd.ms-powerpoint", "ppt");
        add("application/vnd.openxmlformats-officedocument.presentationml.presentation", "pptx");
        add("text/plain", "txt");
        add("application/vnd.ms-excel", "xls");
        add("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "xlsx");

        add("application/vnd.android.package-archive", "apk");
        add("text/css", "css");
        add("text/html", "htm");
        add("text/html", "html");
        add("image/ico", "ico");
        add("application/vnd.ms-powerpoint", "pps");
        add("application/rar", "rar");
        add("application/x-shockwave-flash", "swf");
        add("application/xhtml+xml", "xhtml");
        add("text/xml", "xml");
        add("application/zip", "zip");

    }

    private static void add(String mimeType, String extension) {
        if (!mimeTypeToExtensionMap.containsKey(mimeType)) {
            mimeTypeToExtensionMap.put(mimeType, extension);
        }
        extensionToMimeTypeMap.put(extension, mimeType);
    }

    /**
     * 根据文件后缀名获得对应的MIME类型。
     */
    private static String getMIMEType(File file) {
        String filename = file.getName();
        return getMIMEType(filename);
    }

    public static String getMIMEType(String fileName) {
        int dotIndex = fileName.lastIndexOf(".");
        if (dotIndex < 0) {
            return "*/*";
        }
        /* 获取文件的后缀名 */
        String end = fileName.substring(dotIndex + 1, fileName.length());
        end = end.toLowerCase(Locale.getDefault());
        String type = extensionToMimeTypeMap.get(end);
        if (type == null) {
            return "*/*";
        }
        return type;
    }

    public static boolean isImage(String fileName) {
        return TextUtils.equals(getMIMEType(fileName), "image/*");
    }

    public static void downloadFile(String url) {

        Uri srcUri = Uri.parse(url);

        DownloadManager downloadManager =
                (DownloadManager) BankruptcyApp.getInstance().getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request request = new DownloadManager.Request(srcUri);
        downloadManager.enqueue(request);
    }


    /**
     * 把bitmap转换成String
     *
     * @param filePath
     * @return
     */
    private String SDPATH;

    public FileUtil() {

    }

    public String getSDPATH() {
        return SDPATH;
    }

    public FileUtil(String SDPATH) {
        //得到外部存储设备的目录（/SDCARD）
        SDPATH = Environment.getExternalStorageDirectory() + "/";
    }

    /**
     * 在SD卡上创建文件
     *
     * @param fileName
     * @return
     */
    public File createSDFile(String fileName) throws IOException {
        File file = new File(fileName);
        Boolean flag = file.createNewFile();
        return file;
    }

    /**
     * 在SD卡上创建目录
     *
     * @param dirName 目录名字
     * @return 文件目录
     */
    public File createDir(String dirName) {
        File dir = new File(dirName);
        Boolean flag = dir.mkdir();
        return dir;
    }

    /**
     * 判断文件是否存在
     *
     * @param fileName
     * @return
     */
    public boolean isFileExist(String fileName) {
        File file = new File(fileName);
        return file.exists();
    }

    public File write2SDFromInput(String path, String fileName, InputStream input) {
        File file = null;
        OutputStream output = null;

        try {
            createDir(path);
            file = createSDFile(path + fileName);
            output = new FileOutputStream(file);
            byte[] buffer = new byte[4 * 1024];
            while (input.read(buffer) != -1) {
                output.write(buffer);
                output.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

}
